<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\api\controller\shop;

use app\api\controller\shop\Base;

/**
 * @title 【文章】
 */
class Article extends Base {

    /**
     * @title init
     */
    public function _initialize() {
        parent::_initialize();
        header("Access-Control-Allow-Origin: *");
        header('Access-Control-Allow-Headers:x-requested-with,content-type,tk-uid,tk-token');
    }

    /**
     * @title 服务中心
     * @method post
     * @params last_index 0 初始查询记录
     * @params page_size 10 分页数
     */
    public function lists() {



        $last_index = input('post.last_index', 0);
        $page_size = input('post.page_size', 20);


        $cid = db('article_cat')->where('alias', 'shop_service_center')->value('id');
        $where['cid'] = ['=', $cid];




        $last_index = input('post.last_index', 0);
        $page_size = input('post.page_size', 20);

        $lists = db('article')
                ->field('id,cid,title,description')
                ->where($where)
                ->limit($last_index, $page_size)
                // ->fetchSql(true)
                ->order('id desc')
                ->select();


        return ['code' => 0, 'msg' => 'success', 'data' => $lists];
    }

    /**
     * @title 新闻详情
     * @method post
     * @params id 0
     */
    public function views() {

        $id = input('post.id');

        if ($id) {

            $one = db('article')->where('id', $id)->field('id,title,content,description')->find();

            // 内容里如果有图片，要加上完整路径
            $regex = '/src="(.*?)"/';
            $replacement = 'src="'.APP_HOST.APP_URL.'$1"';
            $one['content'] = preg_replace($regex, $replacement, $one['content']);


            return ['code' => 0, 'msg' => 'success', 'data' => $one];
        } else {
            return ['code' => 1, 'msg' => '没有找到相关信息'];
        }
    }

}
